﻿
$(document).ready(function () {
    //初始化表单验证
    initValidate();

    //获取顺序号
    $("#Pagekey").change(function () {
        var pagekey = $(this).val();
        if (pagekey == "")
            return;

        $.ajax({
            type: "post",
            url: baseurl + "Manage/Gridcolumn/GetNextSort",
            data: {
                pagekey: pagekey
            },
            success: function (e) {
                $("#Sort").val(e);
            },
            error: function (xhr) {
                showError(xhr);
            }
        });
    });

    //获取语言资源key
    $("#Title").change(function () {
        var title = $(this).val();
        if (title == "") return;

        $.ajax({
            type: "post",
            url: baseurl + "Public/GetResKey",
            data: {
                name: title
            },
            success: function (e) {
                if (e != "") {
                    $("#TitleReskey").val(e);
                }
            },
            error: function (xhr) {
                showError(xhr);
            }
        });
    });

    //保存
    $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
    //取消
    $("#btncancel").click(function (evt) { parent.closeDialog(); return false; });
});

function getNextSort(data) {
    $.ajax({
        type: "post",
        url: baseurl + "Manage/Gridcolumn/GetNextSort",
        data: {
            pagekey: data.text
        },
        success: function (e) {
            $("#Sort").val(e);
        },
        error: function (xhr) {
            showError(xhr);
        }
    });
}

//初始化表单验证
function initValidate() {
    //表单验证
    $('#inputform').validate({
        rules: {
            Pagekey: {
                required: true,
                maxlength: 100
            },
            Field: {
                required: true, maxlength: 100
            },
            Title: {
                required: true, maxlength: 100
            },
            Title_en: {
                maxlength: 100
            },
            TitleReskey: {
                maxlength: 100
            },
            Sort: {
                required: true, digits: true
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        messages: {
            Reskey: {
                remote: 'Key已经存在'
            }
        }
    });
}

//保存数据
function save(obj) {
    // debugger;
    if (!$("#inputform").valid()) {
        return;
    }

    var jsondata = {
        Id: $("#Id").val(),
        Values: {}
    };
    //主表数据
    $("#inputform .datafield").each(function () {
        jsondata.Values[$(this).attr("data-fieldname")] = $(this).val();
    });
    jsondata.Values.Pagekey = $("#Pagekey").val();
    jsondata.Values.Sortable = $("#Sortable").prop("checked") ? 1 : 0;
    jsondata.Values.Islist = $("#Islist").prop("checked") ? 1 : 0;
    jsondata.Values.Isexport = $("#Isexport").prop("checked") ? 1 : 0;

    //禁用保存按钮，防止重复提交
    $(obj).attr("disabled", true);

    $.ajax({
        type: "post",
        url: baseurl + "Manage/Gridcolumn/Save",
        data: {
            jsondata: JSON.stringify(jsondata)
        },
        success: function (e) {
            $(obj).attr("disabled", false);
            if (e.Ret == "0") {
                parent.closeDialog(true);
            } else {
                $.messager.alert(csms_lang.message_failure, e.Msg, "error");
            }
        },
        error: function (xhr) {
            $(obj).attr("disabled", false);

            showError(xhr);
        }
    });
}